package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.CategoryMapper;
import com.example.mapper.ProductMapper;
import com.example.model.Category;
import com.example.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryService extends ServiceImpl<CategoryMapper,Category> {
        @Autowired
        CategoryMapper categoryMapper;

        @Autowired
        ProductMapper productMapper;

        public List<Category> caList() {
                QueryWrapper<Category> caWrapper = new QueryWrapper<>();
                caWrapper.eq("is_parent",0);
                List<Category> caList = this.baseMapper.selectList(caWrapper);//先加入最基本的id和name
                for (Category ca: caList) {
                       List<Product> pros = productMapper.proList(ca.getId());//查出对应的cid的product集合。
                        ca.setProducts(pros);//完成产品队列的组合
                }
                return caList;
        }


}
